package tongwentongshu.com.app.activity;

import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.bean.PayEvent;
import tongwentongshu.com.app.contract.RechargeContract;
import tongwentongshu.com.app.presenter.RechargePresenter;
import tongwentongshu.com.app.utils.ToastUtil;

/**
 * Created by XiongBin on 2017/7/3.
 */

public class RechargeActivity extends BaseActivity implements RechargeContract.View, View.OnClickListener {

    @BindView(R.id.ll_20)
    LinearLayout one;
    @BindView(R.id.ll_10)
    LinearLayout two;
    @BindView(R.id.ll_5)
    LinearLayout three;
    @BindView(R.id.tv_shudou_sum)
    TextView shudou;
    @BindView(R.id.tv_agreement)
    TextView agreement;//协议
    @BindView(R.id.btn_next)
    Button next;

    RechargePresenter rechargePresenter;
    String money;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_recharge_shudou;
    }

    @Override
    protected int getTitleId() {
        return R.string.recharge_shudou;
    }

    @Override
    protected void initView() {
        shudou.setText(getIntent().getExtras().getString("shudou"));
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        next.setOnClickListener(this);
        initAgreement();
    }

    @Override
    protected void initData() {
        rechargePresenter = new RechargePresenter(this);

    }

    void initAgreement() {
        String url_0_text = "《充返活动协议》";

        SpannableString spStr = new SpannableString(url_0_text);

        spStr.setSpan(new ClickableSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(getResources().getColor(R.color.text_main));       //设置文件颜色
                ds.setUnderlineText(true);      //设置下划线
            }

            @Override
            public void onClick(View widget) {
                Log.d("", "onTextClick........");
                Intent i = new Intent(mContext, WebViewActivity.class);
                i.putExtra("url", "http://tongwentongshu.cn/index.php?g=&m=Index&a=get_share_rule&typeid=3&type=1");
                startActivity(i);
            }
        }, 0, url_0_text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        agreement.setHighlightColor(Color.TRANSPARENT); //设置点击后的颜色为透明，否则会一直出现高亮
        agreement.append(spStr);
        agreement.setMovementMethod(LinkMovementMethod.getInstance());//开始响应点击事件
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                rechargePresenter.payShudou(mContext, money);
                break;
            case R.id.ll_20:
                money = "20";
                one.setBackgroundResource(R.drawable.btn_blue);
                two.setBackgroundResource(R.color.title_bg);
                three.setBackgroundResource(R.color.title_bg);
                break;
            case R.id.ll_10:
                money = "10";
                one.setBackgroundResource(R.color.title_bg);
                two.setBackgroundResource(R.drawable.btn_blue);
                three.setBackgroundResource(R.color.title_bg);
                break;
            case R.id.ll_5:
                money = "5";
                one.setBackgroundResource(R.color.title_bg);
                two.setBackgroundResource(R.color.title_bg);
                three.setBackgroundResource(R.drawable.btn_blue);
                break;
            default:
                break;
        }
    }

    //支付回调
    public void onEventMainThread(PayEvent result) {
        switch (result.errCode) {
            case 0:
                ToastUtil.show(mContext, "充值成功！");
                finish();
                break;
            case -1:
                ToastUtil.show(mContext, "支付失败");
                break;
            case -2:
                ToastUtil.show(mContext, "支付中断");
                break;
            default:
                break;
        }
    }

}
